package com.windoent.yugioh.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.windoent.yugioh.R;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.SharedPreUtil;
import com.windoent.yugioh.widget.dialog.LoadingDialog;

/**
 * author : robert
 * date   : 2021/4/26 14:52
 * desc   :
 */
public abstract class BaseFragment extends Fragment {
    protected View mView;
    private boolean isInited = false;
    protected Context mContext;
    protected Gson gson = new Gson();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView != null) {
            return mView;
        }
        mView = inflater.inflate(getLayoutId(), container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mContext = getActivity();
        if (!isHidden()) {
            isInited = true;
            initEventAndData();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!isInited && !hidden) {
            isInited = true;
            initEventAndData();
        }
    }

    LoadingDialog loadingDialog;

    /**
     * 显示加载框
     */
    public void showLoadingDialog(String title) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(mContext, BaseMethod.notNullDefaultValue(title, mContext.getString(R.string.string_loading)));
        }
        loadingDialog.show();
    }

    /**
     * 取消加载框
     */
    public void hideLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    protected abstract int getLayoutId();

    protected abstract void initEventAndData();

    public void setText(TextView text, String s) {
        text.setText(BaseMethod.notNullDefaultValueNull(s));
    }

    public void setText(EditText text, String s) {
        text.setText(BaseMethod.notNullDefaultValueNull(s));
    }

    //获取usertoken
    protected String getUserToken() {
        return SharedPreUtil.getString(mContext, Constants.APPTOKEN);
    }

    protected int getAccountId() {
        return SharedPreUtil.getInt(mContext, Constants.USERID, 0);
    }

    //当前位置
    protected String getCurrentLocation() {
        String location = SharedPreUtil.getString(mContext, Constants.LOCATIONCURRENT);
        return BaseMethod.notNullDefaultValue(location, getString(R.string.string_location_defeat));
    }

    //当前位置code
    protected String getCurrentLocationCode() {
        return BaseMethod.notNullDefaultValueNull(SharedPreUtil.getString(mContext, Constants.LOCATIONCURRENTCITYCODE));
    }

    //是否登录
    public boolean isLogIn() {
        return BaseMethod.notNull(getUserToken());
    }


}
